package com.foo.CR;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 * Main class launched when app is launched.
 * Checks if WiFi is ON, if not asks user, and redirects to WiFi settings page.
 * On button press, navigates the user to SelectWiFi Activity
 */
public class ChangeRinger extends Activity
{
    private static String TAG = "CR";
    private Button _addConfig;
    private AlertDialog _alert = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        _addConfig = (Button) findViewById(R.id.butt_add_rule);

        ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        boolean wifiConnected = mWifi.isConnected();
        Log.d(TAG, " Wifi State : " + wifiConnected);

        /* Ask the user to enable wifi */
        if (! wifiConnected)
        {

            /**
             * Note: Even if this is a dialog box, there is no need of any rotation handling.
             * Since, on rotation, the activity will be destroyed and re-created, thus this dialog box will be shown
             * again. The dialog is cancelled in the onDestroy.
             */
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("WiFi is turned OFF. It needs to be ON in order to" +
                    "get the list of WiFi ID's to configure. Do you want to go to WiFi Settings ?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {
                    Intent showWifiSettings = new Intent(Settings.ACTION_WIFI_SETTINGS);
                    startActivity(showWifiSettings);
                }

            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }

            });

            _alert = builder.create();
            _alert.show();
        }

        _addConfig.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent startList = new Intent();
                startList.setClassName("com.foo.CR", "com.foo.CR.SelectWiFi");
                startActivity(startList);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (_alert != null)
        {
            _alert.cancel();
        }
    }

}
